﻿using JinfuyuanProject.Entity;

using JinfuyuanProject.Repository;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JinfuyuanProject.Service
{
    /*
*  
* 母子涵
* 2025-04
*/
    public class PackageService : IPackageService
    {
        private readonly IPackageRepository _packageRepository;

        public PackageService(IPackageRepository packageRepository)
        {
            _packageRepository = packageRepository;
        }
        public bool Delete(Package package)
        {
            return _packageRepository.Delete(package);
        }
        public IQueryable<Package> GetList()
        {
            return _packageRepository.GetList();
        }
        public Package GetModel(int Id)
        {
            return _packageRepository.GetModel(Id);
        }
        public int Insert(Package package)
        {
            return _packageRepository.Insert(package);
        }
        public bool Update(Package package)
        {
            return _packageRepository.Update(package);
        }
        #region 业务方法
        public Hashtable Query(PackageFilter packageFilter)
        {
            int count;
            IQueryable<Package> packageList = GetList();
            Hashtable hashtable = new Hashtable();
            hashtable["code"] = 0;
            hashtable["message"] = "success";
            hashtable["data"] = MakeQueryFilter(packageList, packageFilter, out count);
            hashtable["count"] = count;//结束惰性加载
            return hashtable;
        }
        #endregion
        #region 公共方法
        public IQueryable<Package> MakeQueryFilter(IQueryable<Package> packages, PackageFilter packageFilter, out int count)
        {
            //需要过滤后没做分页前的所有数据条数
            count = packages.Count();
            //做分页
            if (packageFilter.page != 0 && packageFilter.limit != 0)
                return packages.OrderBy(x => x.Id).Skip((packageFilter.page - 1) * packageFilter.limit).Take(packageFilter.limit);
            else
                return packages;
        }
        #endregion
    }
}
